{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "<h1 align=\"center\">\n",
        "  <a href=\"https://portkey.ai\">\n",
        "    <img width=\"300\" src=\"https://analyticsindiamag.com/wp-content/uploads/2023/08/Logo-on-white-background.png\" alt=\"portkey\">\n",
        "  </a>\n",
        "</h1>"
      ],
      "metadata": {
        "id": "zld1izGSx2rl"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "[Portkey](https://app.portkey.ai/) is the Control Panel for AI apps. With it's popular AI Gateway and Observability Suite, hundreds of teams ship reliable, cost-efficient, and fast apps.\n",
        "\n",
        "With Portkey, you can\n",
        "\n",
        "- Connect to 200+ models through a unified API,\n",
        "- View 40+ metrics & logs for all requests\n",
        "- Enable semantic cache to reduce latency & costs\n",
        "- Implement automatic retries & fallbacks for failed requests"
      ],
      "metadata": {
        "id": "VfWXytp_1EVj"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "This notebook demonstrates how to use Portkey for function calling with various LLM providers including OpenAI, Anthropic, and Google's Gemini."
      ],
      "metadata": {
        "id": "F1zYiehz1VAl"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1TeqYiXhlfZvFhoOHWEiyBycfWu0arWwp?usp=sharing)"
      ],
      "metadata": {
        "id": "MosN-n4Wx4ZP"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install -qU openai portkey-ai"
      ],
      "metadata": {
        "id": "lha5CiI-kvcu"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "tools = [\n",
        "        {\n",
        "            \"type\": \"function\",\n",
        "            \"function\": {\n",
        "                \"name\": \"get_current_weather\",\n",
        "                \"description\": \"Get the current weather in a given location\",\n",
        "                \"parameters\": {\n",
        "                    \"type\": \"object\",\n",
        "                    \"properties\": {\n",
        "                        \"location\": {\n",
        "                            \"type\": \"string\",\n",
        "                            \"description\": \"The city and state, e.g. San Francisco, CA\",\n",
        "                        },\n",
        "                        \"unit\": {\"type\": \"string\", \"enum\": [\"celsius\", \"fahrenheit\"]},\n",
        "                    },\n",
        "                    \"required\": [\"location\"],\n",
        "                },\n",
        "            },\n",
        "        }\n",
        "    ]"
      ],
      "metadata": {
        "id": "_wviaUs5pi8B"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "restaurant_recommendations = [{\n",
        "        \"type\": \"function\",\n",
        "        \"function\": {\n",
        "            \"name\": \"get_restaurant_recommendations\",\n",
        "            \"description\": \"Get restaurant recommendations for a given location and cuisine type\",\n",
        "            \"parameters\": {\n",
        "                \"type\": \"object\",\n",
        "                \"properties\": {\n",
        "                    \"location\": {\n",
        "                        \"type\": \"string\",\n",
        "                        \"description\": \"The city and state, e.g. New York, NY\"\n",
        "                    },\n",
        "                    \"cuisine\": {\n",
        "                        \"type\": \"string\",\n",
        "                        \"description\": \"The type of cuisine, e.g. Italian, Chinese, Mexican\"\n",
        "                    },\n",
        "                    \"price_range\": {\n",
        "                        \"type\": \"string\",\n",
        "                        \"enum\": [\"$\", \"$$\", \"$$$\", \"$$$$\"],\n",
        "                        \"description\": \"The price range of the restaurants\"\n",
        "                    }\n",
        "                },\n",
        "                \"required\": [\"location\", \"cuisine\"]\n",
        "            }\n",
        "        }\n",
        "    },\n",
        "    ]"
      ],
      "metadata": {
        "id": "bLeShK0bTj_r"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "latest_ai_news =  [{\n",
        "    \"type\": \"function\",\n",
        "    \"function\": {\n",
        "        \"name\": \"get_latest_ai_news\",\n",
        "        \"description\": \"Retrieve the latest news articles about artificial intelligence\",\n",
        "        \"parameters\": {\n",
        "            \"type\": \"object\",\n",
        "            \"properties\": {\n",
        "                \"topic\": {\n",
        "                    \"type\": \"string\",\n",
        "                    \"description\": \"Specific AI topic (optional, e.g., 'machine learning', 'neural networks', 'robotics')\"\n",
        "                },\n",
        "                \"timeframe\": {\n",
        "                    \"type\": \"string\",\n",
        "                    \"enum\": [\"today\", \"this_week\", \"this_month\"],\n",
        "                    \"description\": \"Time period for the news articles\"\n",
        "                },\n",
        "                \"source_type\": {\n",
        "                    \"type\": \"string\",\n",
        "                    \"enum\": [\"all\", \"academic\", \"industry\", \"mainstream\"],\n",
        "                    \"description\": \"Type of news sources to include\"\n",
        "                },\n",
        "                \"max_results\": {\n",
        "                    \"type\": \"integer\",\n",
        "                    \"description\": \"Maximum number of news articles to return (1-50)\",\n",
        "                    \"minimum\": 1,\n",
        "                    \"maximum\": 50\n",
        "                },\n",
        "                \"language\": {\n",
        "                    \"type\": \"string\",\n",
        "                    \"description\": \"Preferred language for the articles (e.g., 'en' for English, 'es' for Spanish)\"\n",
        "                }\n",
        "            },\n",
        "            \"required\": [\"timeframe\"]\n",
        "        }\n",
        "    }\n",
        "}]"
      ],
      "metadata": {
        "id": "kG-wPaBvXDH3"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Initialize Portkey client\n",
        "from portkey_ai import Portkey\n",
        "from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders\n",
        "from google.colab import userdata\n",
        "from tabulate import tabulate\n",
        "\n",
        "providers = [\n",
        "        (\"openai\", \"gpt-4o\"),\n",
        "         (\"openai\", \"gpt-4o-mini\"),\n",
        "        (\"anthropic\", \"claude-3-5-sonnet-20240620\"),\n",
        "        (\"anthropic\", \"claude-3-opus-20240229\"),\n",
        "        (\"anthropic\", \"claude-3-haiku-20240307\"),\n",
        "        (\"google\", \"gemini-1.5-flash-latest\"),\n",
        "        (\"google\", \"gemini-1.5-pro\"),\n",
        "        #(\"fireworks-ai\", \"accounts/yi-01-ai/models/yi-large\"),\n",
        "        (\"fireworks-ai\", \"accounts/fireworks/models/firefunction-v2\"),\n",
        "        # (\"fireworks-ai\", \"accounts/fireworks/models/codegemma-2b\"),\n",
        "        #  (\"fireworks-ai\", \"accounts/fireworks/models/llama-v2-7b\"),\n",
        "        #(\"groq\", \"llama3-groq-70b-8192-tool-use-preview\"),\n",
        "\n",
        "   #  (\"together-ai\", \"meta-llama/Llama-2-70b-hf\"),  #-  Error code: 400 - {'error': {'message': 'together-ai error: google/gemma-2-27b-it is not supported for JSON mode/function calling\n",
        "\n",
        "\n",
        "    ]\n",
        "\n",
        "\n",
        "portkey = Portkey(api_key=userdata.get('PORTKEY_API_KEY'))\n",
        "\n",
        "\n",
        "def portkey_function_call(messages):\n",
        "    results = []\n",
        "\n",
        "    for provider, model in providers:\n",
        "        config = {\n",
        "            \"provider\": provider.lower(),\n",
        "            \"api_key\": userdata.get(f'{provider.upper()}_API_KEY')\n",
        "        }\n",
        "\n",
        "\n",
        "        # Make the API call\n",
        "        response = portkey.with_options(config=config).chat.completions.create(\n",
        "            model=model,\n",
        "            messages=messages,\n",
        "            tools=latest_ai_news,\n",
        "            tool_choice=\"auto\",\n",
        "            max_tokens=512,\n",
        "        )\n",
        "\n",
        "        response_message = response.choices[0].message\n",
        "        tool_call_response = str(response_message.tool_calls) if response_message.tool_calls else \"No tool calls\"\n",
        "\n",
        "        results.append([model, provider, tool_call_response])\n",
        "\n",
        "    # Print results using tabulate\n",
        "    print(tabulate(results, headers=[\"Model\", \"Provider\", \"Tool Call Response\"], tablefmt=\"grid\"))"
      ],
      "metadata": {
        "id": "PRj-hPyO6jVt"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "#Testing of Get Latest AI News Tool"
      ],
      "metadata": {
        "id": "2m_RKBxtYQFL"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Test message\n",
        "messages = [{\"role\": \"user\", \"content\": \"give me latest ai news of this week\"}]\n",
        "\n",
        "# Run function calling for all providers\n",
        "portkey_function_call(messages)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ySeRNzxRXQ7a",
        "outputId": "a353b7f3-babe-4d83-a459-09e4e9c932e2"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "+-------------------------------------------+--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| Model                                     | Provider     | Tool Call Response                                                                                                                                                                                                                       |\n",
            "+===========================================+==============+==========================================================================================================================================================================================================================================+\n",
            "| gpt-4o                                    | openai       | [ChatCompletionMessageToolCall(id='call_9zk75e22eO5NVzq42vAFmawo', function=FunctionCall(arguments='{\"timeframe\":\"this_week\",\"max_results\":5}', name='get_latest_ai_news'), type='function')]                                            |\n",
            "+-------------------------------------------+--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| gpt-4o-mini                               | openai       | [ChatCompletionMessageToolCall(id='call_Geq0M1hYFgJmNmp6XbonxcmP', function=FunctionCall(arguments='{\"timeframe\":\"this_week\",\"max_results\":5}', name='get_latest_ai_news'), type='function')]                                            |\n",
            "+-------------------------------------------+--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| claude-3-5-sonnet-20240620                | anthropic    | [ChatCompletionMessageToolCall(id='toolu_01LmVxtbDtSaQXq27pX9zeCH', function=FunctionCall(arguments='{\"timeframe\":\"this_week\",\"max_results\":10,\"language\":\"en\"}', name='get_latest_ai_news'), type='function')]                          |\n",
            "+-------------------------------------------+--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| claude-3-opus-20240229                    | anthropic    | [ChatCompletionMessageToolCall(id='toolu_016DEvWVbfCYDNAvntbhCLE7', function=FunctionCall(arguments='{\"timeframe\":\"this_week\"}', name='get_latest_ai_news'), type='function')]                                                           |\n",
            "+-------------------------------------------+--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| claude-3-haiku-20240307                   | anthropic    | [ChatCompletionMessageToolCall(id='toolu_01UzArrKvobYz6Bo2CkG9AZW', function=FunctionCall(arguments='{\"timeframe\":\"this_week\",\"max_results\":10,\"source_type\":\"all\"}', name='get_latest_ai_news'), type='function')]                      |\n",
            "+-------------------------------------------+--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| gemini-1.5-flash-latest                   | google       | [ChatCompletionMessageToolCall(id='portkey-69bbbb43-9bd8-442f-a9be-aa926b46458c', function=FunctionCall(arguments='{\"timeframe\":\"this_week\"}', name='get_latest_ai_news'), type='function')]                                             |\n",
            "+-------------------------------------------+--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| gemini-1.5-pro                            | google       | [ChatCompletionMessageToolCall(id='portkey-4424fd91-e7c1-4eed-ac7a-8be4b52e0325', function=FunctionCall(arguments='{\"timeframe\":\"this_week\"}', name='get_latest_ai_news'), type='function')]                                             |\n",
            "+-------------------------------------------+--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| accounts/fireworks/models/firefunction-v2 | fireworks-ai | [ChatCompletionMessageToolCall(id='call_927xwUgfmPh5vRKqsUoEMVeK', function=FunctionCall(arguments='{\"timeframe\": \"this_week\", \"max_results\": 5, \"source_type\": \"all\", \"language\": \"en\"}', name='get_latest_ai_news'), type='function')] |\n",
            "+-------------------------------------------+--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "#Testing of restaurant_recommendations Tool"
      ],
      "metadata": {
        "id": "-3xJxlWxYZUL"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Test message\n",
        "messages = [{\"role\": \"user\", \"content\": \"Suggest latest and best restaurant of Aurangabad?\"}]\n",
        "\n",
        "# Run function calling for all providers\n",
        "portkey_function_call(messages)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Rjmzq6yXUKGc",
        "outputId": "aa9b0fce-cc6e-4588-e9df-abce595d8abc"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "+-------------------------------------------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| Model                                     | Provider     | Tool Call Response                                                                                                                                                                                                                                              |\n",
            "+===========================================+==============+=================================================================================================================================================================================================================================================================+\n",
            "| gpt-4o                                    | openai       | No tool calls                                                                                                                                                                                                                                                   |\n",
            "+-------------------------------------------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| gpt-4o-mini                               | openai       | [ChatCompletionMessageToolCall(id='call_LuP8f1JvDdmKO5hOI5xoqsNs', function=FunctionCall(arguments='{\"topic\":\"restaurant\",\"timeframe\":\"this_month\",\"source_type\":\"mainstream\",\"max_results\":5,\"language\":\"en\"}', name='get_latest_ai_news'), type='function')]  |\n",
            "+-------------------------------------------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| claude-3-5-sonnet-20240620                | anthropic    | No tool calls                                                                                                                                                                                                                                                   |\n",
            "+-------------------------------------------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| claude-3-opus-20240229                    | anthropic    | No tool calls                                                                                                                                                                                                                                                   |\n",
            "+-------------------------------------------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| claude-3-haiku-20240307                   | anthropic    | [ChatCompletionMessageToolCall(id='toolu_018RjfwRm3xTPzWffnCKid35', function=FunctionCall(arguments='{\"language\":\"en\",\"max_results\":5,\"source_type\":\"all\",\"timeframe\":\"today\",\"topic\":\"aurangabad restaurants\"}', name='get_latest_ai_news'), type='function')] |\n",
            "+-------------------------------------------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| gemini-1.5-flash-latest                   | google       | No tool calls                                                                                                                                                                                                                                                   |\n",
            "+-------------------------------------------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| gemini-1.5-pro                            | google       | No tool calls                                                                                                                                                                                                                                                   |\n",
            "+-------------------------------------------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| accounts/fireworks/models/firefunction-v2 | fireworks-ai | No tool calls                                                                                                                                                                                                                                                   |\n",
            "+-------------------------------------------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "#Testing of Current Weather Featching Tool"
      ],
      "metadata": {
        "id": "60wn4j8DYjU_"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Test message\n",
        "messages = [{\"role\": \"user\", \"content\": \"How's the weather like in San Francisco?\"}]\n",
        "\n",
        "# Run function calling for all providers\n",
        "portkey_function_call(messages)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "kAYKGo137cwP",
        "outputId": "81fd7e38-2c0f-49d5-e1a7-8d2d45b38fc7"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "+-------------------------------------------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| Model                                     | Provider     | Tool Call Response                                                                                                                                                                                                                                                                            |\n",
            "+===========================================+==============+===============================================================================================================================================================================================================================================================================================+\n",
            "| gpt-4o                                    | openai       | [ChatCompletionMessageToolCall(id='call_39osRa58C5UZfcZl2IwTeO8b', function=FunctionCall(arguments='{\\n  \"topic\": \"weather forecasting\",\\n  \"timeframe\": \"today\",\\n  \"source_type\": \"mainstream\",\\n  \"max_results\": 1,\\n  \"language\": \"en\"\\n}', name='get_latest_ai_news'), type='function')] |\n",
            "+-------------------------------------------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| gpt-4o-mini                               | openai       | No tool calls                                                                                                                                                                                                                                                                                 |\n",
            "+-------------------------------------------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| claude-3-5-sonnet-20240620                | anthropic    | No tool calls                                                                                                                                                                                                                                                                                 |\n",
            "+-------------------------------------------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| claude-3-opus-20240229                    | anthropic    | No tool calls                                                                                                                                                                                                                                                                                 |\n",
            "+-------------------------------------------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| claude-3-haiku-20240307                   | anthropic    | No tool calls                                                                                                                                                                                                                                                                                 |\n",
            "+-------------------------------------------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| gemini-1.5-flash-latest                   | google       | No tool calls                                                                                                                                                                                                                                                                                 |\n",
            "+-------------------------------------------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| gemini-1.5-pro                            | google       | No tool calls                                                                                                                                                                                                                                                                                 |\n",
            "+-------------------------------------------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| accounts/fireworks/models/firefunction-v2 | fireworks-ai | No tool calls                                                                                                                                                                                                                                                                                 |\n",
            "+-------------------------------------------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Test message\n",
        "messages = [{\"role\": \"user\", \"content\": \"What's the weather like in San Francisco, Tokyo, and Paris?\"}]\n",
        "\n",
        "# Run function calling for all providers\n",
        "portkey_function_call(messages)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ZrDT-TFv62HW",
        "outputId": "f149c47e-d4c6-4e2b-db97-c8aab2760dcc"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "+-------------------------------------------+--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| Model                                     | Provider     | Tool Call Response                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |\n",
            "+===========================================+==============+========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================+\n",
            "| gpt-4o                                    | openai       | [ChatCompletionMessageToolCall(id='call_jQM4yp3iVovjYBmBrdN74loM', function=FunctionCall(arguments='{\"location\": \"San Francisco\"}', name='get_current_weather'), type='function'), ChatCompletionMessageToolCall(id='call_w8ZYduT9EsP3NqJljv3T7Eyy', function=FunctionCall(arguments='{\"location\": \"Tokyo\"}', name='get_current_weather'), type='function'), ChatCompletionMessageToolCall(id='call_96F45BQiUOj0q4Gy8iKRGsZ7', function=FunctionCall(arguments='{\"location\": \"Paris\"}', name='get_current_weather'), type='function')]                                                                                                                                                                 |\n",
            "+-------------------------------------------+--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| gpt-4o-mini                               | openai       | [ChatCompletionMessageToolCall(id='call_FSiM3gfLKr108PsegkggycFw', function=FunctionCall(arguments='{\"topic\": \"weather\", \"timeframe\": \"today\", \"max_results\": 1, \"language\": \"en\"}', name='get_latest_ai_news'), type='function'), ChatCompletionMessageToolCall(id='call_8Ovrkm2tDdLaauoUaYVqmNYv', function=FunctionCall(arguments='{\"topic\": \"weather\", \"timeframe\": \"today\", \"max_results\": 1, \"language\": \"en\"}', name='get_latest_ai_news'), type='function'), ChatCompletionMessageToolCall(id='call_yKfJ6FFLY12JwiWzXen0LnLP', function=FunctionCall(arguments='{\"topic\": \"weather\", \"timeframe\": \"today\", \"max_results\": 1, \"language\": \"en\"}', name='get_latest_ai_news'), type='function')] |\n",
            "+-------------------------------------------+--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| claude-3-5-sonnet-20240620                | anthropic    | No tool calls                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |\n",
            "+-------------------------------------------+--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| claude-3-opus-20240229                    | anthropic    | No tool calls                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |\n",
            "+-------------------------------------------+--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| claude-3-haiku-20240307                   | anthropic    | [ChatCompletionMessageToolCall(id='toolu_01EbJeB9eYvKG7ANQKkhS9bP', function=FunctionCall(arguments='{\"locations\":\"[\\\\\"San Francisco\\\\\", \\\\\"Tokyo\\\\\", \\\\\"Paris\\\\\"]\"}', name='get_weather'), type='function')]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |\n",
            "+-------------------------------------------+--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| gemini-1.5-flash-latest                   | google       | No tool calls                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |\n",
            "+-------------------------------------------+--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| gemini-1.5-pro                            | google       | No tool calls                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |\n",
            "+-------------------------------------------+--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| accounts/fireworks/models/firefunction-v2 | fireworks-ai | [ChatCompletionMessageToolCall(id='call_B2EiSi4DGpkwTjZhRrBKYDXJ', function=FunctionCall(arguments='{\"topic\": \"weather\", \"timeframe\": \"today\", \"max_results\": 1, \"language\": \"en\"}', name='get_latest_ai_news'), type='function')]                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |\n",
            "+-------------------------------------------+--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Test message\n",
        "messages = [{\"role\": \"user\", \"content\": \"What's the weather like in Mumbai ,Delhi and Banglore?\"}]\n",
        "\n",
        "# Run function calling for all providers\n",
        "portkey_function_call(messages)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "jMv-2PMrVST6",
        "outputId": "7a58e247-9a55-4d4e-e72e-98e8d1c469a6"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "+-------------------------------------------+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| Model                                     | Provider     | Tool Call Response                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |\n",
            "+===========================================+==============+=========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================+\n",
            "| gpt-4o                                    | openai       | [ChatCompletionMessageToolCall(id='call_RDwPeeHLHI9c5MBOxXnRkUlQ', function=FunctionCall(arguments='{\"location\": \"Mumbai\"}', name='get_current_weather'), type='function'), ChatCompletionMessageToolCall(id='call_nP0eZZISJeyzXI1utBG6alLj', function=FunctionCall(arguments='{\"location\": \"Delhi\"}', name='get_current_weather'), type='function'), ChatCompletionMessageToolCall(id='call_pOnq4tp9sSSPAaiarGsvykNd', function=FunctionCall(arguments='{\"location\": \"Bangalore\"}', name='get_current_weather'), type='function')]                                                                                                                                                                                                                                     |\n",
            "+-------------------------------------------+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| gpt-4o-mini                               | openai       | [ChatCompletionMessageToolCall(id='call_vS2RKIJ6Acm3l1lz4HhwOXss', function=FunctionCall(arguments='{\"topic\": \"Mumbai\", \"timeframe\": \"today\", \"source_type\": \"all\", \"max_results\": 1, \"language\": \"en\"}', name='get_latest_ai_news'), type='function'), ChatCompletionMessageToolCall(id='call_i9gg5uKxQaq9XdBaPCcJnnAX', function=FunctionCall(arguments='{\"topic\": \"Delhi\", \"timeframe\": \"today\", \"source_type\": \"all\", \"max_results\": 1, \"language\": \"en\"}', name='get_latest_ai_news'), type='function'), ChatCompletionMessageToolCall(id='call_cRQyEpj3OHoxOuqWnVHDF3Ey', function=FunctionCall(arguments='{\"topic\": \"Bangalore\", \"timeframe\": \"today\", \"source_type\": \"all\", \"max_results\": 1, \"language\": \"en\"}', name='get_latest_ai_news'), type='function')] |\n",
            "+-------------------------------------------+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| claude-3-5-sonnet-20240620                | anthropic    | No tool calls                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |\n",
            "+-------------------------------------------+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| claude-3-opus-20240229                    | anthropic    | No tool calls                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |\n",
            "+-------------------------------------------+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| claude-3-haiku-20240307                   | anthropic    | [ChatCompletionMessageToolCall(id='toolu_018NHHkq5s62Rb8G4bmeZYo1', function=FunctionCall(arguments='{\"language\":\"en\",\"max_results\":3,\"source_type\":\"all\",\"timeframe\":\"today\",\"topic\":\"weather forecast mumbai delhi bangalore\"}', name='get_latest_ai_news'), type='function')]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n",
            "+-------------------------------------------+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| gemini-1.5-flash-latest                   | google       | No tool calls                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |\n",
            "+-------------------------------------------+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| gemini-1.5-pro                            | google       | No tool calls                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |\n",
            "+-------------------------------------------+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| accounts/fireworks/models/firefunction-v2 | fireworks-ai | [ChatCompletionMessageToolCall(id='call_IqRs42JAXIxof45YAcAMWLIB', function=FunctionCall(arguments='{\"topic\": \"none\", \"timeframe\": \"today\", \"source_type\": \"all\", \"max_results\": 3, \"language\": \"en\"}', name='get_latest_ai_news'), type='function')]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |\n",
            "+-------------------------------------------+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n"
          ]
        }
      ]
    }
  ]
}